package com.seven.ezpermissions.helper

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.seven.ezpermissions.OnDeniedCallback
import com.seven.ezpermissions.OnDestroyEvent

/**
 * 说明
 * @author seven.xie
 * @date 2020/7/10 15:46
 * email: se7en.xie@qq.com
 */
class LowApiPermissionsHelper<T> constructor(host: T) : PermissionsHelper<T>(host) {

    override fun directRequest(requestCode: Int, permissions: Array<out String>) {
        throw IllegalStateException("Should never be requesting permissions on API < 23!")
    }

    override fun shouldShowRationale(permission: String): Boolean {
        return false
    }

    override fun showRationale(
        rationale: String,
        positiveButton: String,
        negativeButton: String,
        requestCode: Int,
        permissions: Array<out String>,
        onDeniedCallback: OnDeniedCallback?
    ) {
        throw IllegalStateException("Should never be requesting permissions on API < 23!")
    }

    override fun addLifecycleObserver(observer: LifecycleObserver) {
        // do nothing
    }

    override fun onStateChanged(
        source: LifecycleOwner,
        event: Lifecycle.Event,
        observer: LifecycleObserver,
        callback: OnDestroyEvent
    ) {
        // do nothing
    }

    override val context: Context?
        get() = when (host) {
            is AppCompatActivity -> {
                host as Context
            }
            is Fragment -> {
                (host as Fragment).context
            }
            else -> {
                throw IllegalStateException("Unkown host: $host")
            }
        }

}